package com.njitzx.service.impl;

import com.njitzx.entity.po.Permission;
import com.njitzx.entity.vo.PermVO;
import com.njitzx.mapper.PermissionMapper;
import com.njitzx.service.PermissionService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {

    private final PermissionMapper permissionMapper;

    /**
     * 权限表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param permission
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Permission> findPage(Integer pageNum, Integer pageSize, Permission permission) {
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> permissionList = permissionMapper.select(permission);
        return new PageInfo<>(permissionList);
    }

    /**
     * 根据主键查询权限表
     *
     * @param psId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Permission findById(Serializable psId) {
        return permissionMapper.selectById(psId);
    }

    /**
     * 权限表新增
     *
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }

    /**
     * 修改权限表
     *
     * @param permission
     */
    @Override
    public void modify(Permission permission) {
        permissionMapper.update(permission);
    }

    /**
     * 根据主键删除权限表
     *
     * @param psId
     */
    @Override
    public void removeById(Serializable psId) {
        if (((String) psId).contains(",")) {
            permissionMapper.deleteByIds(((String) psId).split(","));
        } else {
            permissionMapper.deleteById(psId);
        }
    }

    @Override
    public List<PermVO> findAll(String type) {
        //思路  首先查询所有的数据 这个数据并不是返回的数据
        List<Permission> permissions = permissionMapper.select(new Permission());

        if ("list".equals(type)) {
            List<PermVO> collect = permissions.stream().map(this::getPermVO)
                    .collect(Collectors.toList());
            return collect;
        }
        if ("tree".equals(type)) {
            List<PermVO> collect = permissions.stream().filter(item -> item.getPsPid() == 0) //拿到父接待你 对父接待你进行递归哦
                    .map(item -> {
                        return getPermVOFN(item, permissions);
                    }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    private PermVO getPermVOFN(Permission permission, List<Permission> permissions) {
        PermVO permVO1 = getPermVO(permission);  //变为vo
        List<PermVO> children = permissions.stream()
                .filter(item -> item.getPsPid().equals(permission.getPsId()))  //ru过找到了和
                .map(item -> {
                    return getPermVOFN(item, permissions);
                }).collect(Collectors.toList());

        permVO1.setChildren(children);

        return permVO1;
    }

    private PermVO getPermVO(Permission item) {
        PermVO permVO = new PermVO();
        permVO.setId(item.getPsId());
        permVO.setAuthName(item.getPsName());
        permVO.setLevel(item.getPsLevel());
        permVO.setPath(item.getPsApiPath());
        permVO.setPid(item.getPsPid());
        return permVO;
    }
}
